﻿/************************************************
*
* Copyright(c) ZeeGuo.com
*
* CLR : 4.0.30319.296
*
* FileName : HttpHelper.cs
*
* Author : Kurodo
*
* CreatTime : 2013/6/24 14:26:35
*           
*************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace ComicSpider
{
    public class HttpHelper
    {
        #region 私有变量
        private string _contentType = "application/x-www-form-urlencoded";
        private string _accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
        private string _userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
        private CookieContainer _cookieContainer;
        private Encoding _encoding = Encoding.UTF8;
        private HttpWebRequest _httpWebRequest;
        private HttpWebResponse _httpWebResponse;
        #endregion

        #region 属性
        /// <summary>
        /// Http内容类型
        /// </summary>
        public string ContentType
        {
            set
            {
                _contentType = value;
            }

            get
            {
                return _contentType;
            }
        }

        /// <summary>
        /// 接受文件类型
        /// </summary>
        public string Accept
        {
            set
            {
                _accept = value;
            }
            get
            {
                return _accept;
            }
        }

        /// <summary>
        /// 特殊字符串头
        /// </summary>
        public string UserAgent
        {
            set
            {
                _userAgent = value;
            }
            get
            {
                return _userAgent;
            }
        }

        /// <summary>
        /// Cookie集合
        /// </summary>
        public CookieContainer CookieContainer
        {
            get
            {
                return _cookieContainer;
            }
        }

        /// <summary>
        /// 页面内容编码
        /// </summary>
        public Encoding Encoding
        {
            get
            {
                return _encoding;
            }
            set
            {
                _encoding = value;
            }
        }
        #endregion

        #region 构造方法
        public HttpHelper()
        {
            _cookieContainer = new CookieContainer();
        }

        public HttpHelper(CookieContainer cc)
        {
            _cookieContainer = cc;
        }

        public HttpHelper(string contentType, string accept, string userAgent)
        {
            _contentType = contentType;
            _accept = accept;
            _userAgent = userAgent;
            _cookieContainer = new CookieContainer();
        }

        public HttpHelper(string contentType, string accept, string userAgent, CookieContainer cc)
        {
            _contentType = contentType;
            _accept = accept;
            _userAgent = userAgent;
            _cookieContainer = cc;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">超链接</param>
        /// <param name="postData">POST数据</param>
        /// <param name="method">请求类型</param>
        /// <param name="cc">Cookie集合</param>
        /// <returns></returns>
        public string Request(string url, string postData, HttpRequestMethod method, CookieContainer cc)
        {
            if (string.IsNullOrEmpty(postData))
            {
                return Request(url, cc);
            }

            byte[] byteRequest = Encoding.Default.GetBytes(postData);

            _httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

            _httpWebRequest.CookieContainer = cc;
            _httpWebRequest.ContentType = _contentType;
            _httpWebRequest.Referer = url;
            _httpWebRequest.Accept = _accept;
            _httpWebRequest.UserAgent = _userAgent;
            if (method == HttpRequestMethod.Post)
            {
                _httpWebRequest.Method = "POST";
            }
            else
            {
                _httpWebRequest.Method = "GET";
            }
            _httpWebRequest.ContentLength = byteRequest.Length;

            Stream stream = _httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();

            _httpWebResponse = (HttpWebResponse)_httpWebRequest.GetResponse();
            Stream respStream = _httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(respStream, _encoding);
            string result = streamReader.ReadToEnd();
            respStream.Close();
            streamReader.Close();

            return result;
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">超链接</param>
        /// <param name="cc">Cookie集合</param>
        /// <returns></returns>
        public string Request(string url, CookieContainer cc)
        {
            _httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            _httpWebRequest.CookieContainer = cc;
            _httpWebRequest.ContentType = _contentType;
            _httpWebRequest.Referer = url;
            _httpWebRequest.Accept = _accept;
            _httpWebRequest.UserAgent = _userAgent;
            _httpWebRequest.Method = "GET";

            _httpWebResponse = (HttpWebResponse)_httpWebRequest.GetResponse();
            Stream respStream = _httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(respStream, _encoding);
            string result = streamReader.ReadToEnd();
            respStream.Close();
            streamReader.Close();

            return result;
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">超链接</param>
        /// <returns></returns>
        public string Request(string url)
        {
            return Request(url, _cookieContainer);
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">超链接</param>
        /// <param name="postData">POST数据</param>
        /// <param name="method">请求类型</param>
        /// <returns></returns>
        public string Request(string url, string postData, HttpRequestMethod method)
        {
            return Request(url, postData, method, _cookieContainer);
        }

        /// <summary>
        /// 返回标头内容
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <returns></returns>
        public string GetResponseHeader(string name)
        {
            return _httpWebResponse.GetResponseHeader(name);
        }

        /// <summary>
        /// 返回标头内容
        /// </summary>
        /// <param name="url">超链接</param>
        /// <param name="name">参数名称</param>
        /// <returns></returns>
        public string GetResponseHeader(string url, string name)
        {
            if (string.IsNullOrEmpty(url))
            {
                return GetResponseHeader(name);
            }

            Request(url);
            return _httpWebResponse.GetResponseHeader(name);
        }
        #endregion
    }

    public enum HttpRequestMethod
    {
        Get = 0,
        Post = 1
    }
}
